import { devDebug } from '../../../utils/settings'

Page({
  data: {
    balance: null,
    balanceList: [],
  },
  onLoad(options) {
    this.loadBalanceData();
  },
  async loadBalanceData() {
    const res = await new Promise(resolve => {
      wx.cloud.callFunction({
        name: 'db_balance',
        data: {
          funcName: 'query',
          searchText: '',
          searchData: {},
        },
        success: (r) => resolve(r.result),
      });
    })
    if (devDebug) console.log('余额', res);
    const user_id = res.balance._id;
    this.setData({
      current_balance: (res.balance.current_balance / 100).toFixed(2),
      balanceList: res.list.map(o => {
        o.modified_balance = (user_id == o.payer_id ? o.payer_balance : o.payee_balance);
        o.change_balance = ((user_id == o.payer_id ? -1 : 1) * (o.change_balance / 100)).toFixed(2);
        o.modified_balance = (o.modified_balance / 100).toFixed(2);
        o.payer_balance = (o.payer_balance / 100).toFixed(2);
        o.modified_time = new Date(o.modified_time).Format();
        return o;
      })
    })
  }
})